import Immutable from 'immutable';
import { queryVipModal, isVip } from '../services/vip';
// import toast from '../utils/toast';

const VipRecord = Immutable.Record({
  id: null,
  amount: null,
  amountYuan: null,
  vipType: null,
  cardName: null,
  selected: false,
  endTime: null,
  cardId: null, // 卡类型 1金卡 2银卡 3黑卡 4钻石卡 5至尊卡
  limitMonthMoneyYuan: null,
  useMoneyYuan: null,
  remainMoneyYuan: null,
});

export class Vip extends VipRecord {

}

export default {
  namespace: 'vip',
  state: Immutable.fromJS({
    modal: {
      show: false,
      vip: null,
    },
    vip: {
      isVip: false,
      list: [],
    },
  }),

  effects: {
    *fetchVipModal({ payload }, { call, put }) {
      const res = yield call(queryVipModal, payload);
      if (res.success) {
        yield put({
          type: 'updateVipModal',
          payload: res.data,
        });
      }
    },
    *fetchUserVip({ payload }, { call, put }) {
      const res = yield call(isVip, payload);
      if (res.success) {
        yield put({
          type: 'updateVip',
          payload: res.data,
        });
      }
    },
  },

  reducers: {
    closeVipModal(state) {
      return state.setIn(['modal', 'show'], false);
    },
    updateVipModal(state, { payload }) {
      return state.mergeIn(['modal'], {
        show: payload.status === 1,
        vip: new Vip(payload),
      });
    },
    selectVip(state) {
      const selected = state.getIn(['vip', 'list', 0, 'selected']);
      return state.setIn(['vip', 'list', 0, 'selected'], !selected);
    },
    updateVip(state, { payload }) {
      return state.mergeIn(['vip'], {
        isVip: payload.status === 1,
        list: payload.userMemberTypeList.map(item => new Vip(item)),
      });
    },
  },
};
